package com.chang.bootplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chang.bootplus.base.annotation.PublicVisit;
import com.chang.bootplus.base.exception.BusException;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.base.utils.BeanUtil;
import com.chang.bootplus.entity.MatrixUser;
import com.chang.bootplus.entity.UserLocation;
import com.chang.bootplus.po.*;
import com.chang.bootplus.service.MatrixUserService;
import com.chang.bootplus.service.UserLocationService;
import com.chang.bootplus.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chang
 * @since 2022-01-26
 */
@Api(tags = {"用户信息"})
@RestController
@RequestMapping("/matrix-user")
public class MatrixUserController {


    @Autowired
    MatrixUserService userService;



    @Autowired
    UserLocationService userLocationService;

    // 根据id进行更新

    @PublicVisit
    @PostMapping("/login")
    @ApiOperation(httpMethod = "POST", value = "前台登录接口")
    public ApiResponse<Void> login(@RequestBody LoginPO po){
        return new ApiResponse();
    }


    @PublicVisit
    @PostMapping("/admin/login")
    @ApiOperation(httpMethod = "POST", value = "后台登录接口")
    public ApiResponse<Void> adminlogin(@RequestBody LoginAdminPO po){
        return new ApiResponse();
    }

    // 根据id进行更新
    @PostMapping("/user/register")
    @PublicVisit
    @ApiOperation(httpMethod = "POST", value = "注册接口")
    public ApiResponse<Void> register(@RequestBody RegisterPO po){
        return userService.register(po);
    }


    // 根据id进行更新
    @PostMapping("/user/updateUserInfo")
    @ApiOperation(httpMethod = "POST", value = "修改用户信息")
    public ApiResponse<Void> updateUserInfo(@RequestBody UserUpdatePO po){
        MatrixUser matrixUser = BeanUtil.beanA2beanB(po, MatrixUser.class);
        matrixUser.setId(po.getUserId());
        userService.updateById(matrixUser);
        return new ApiResponse();
    }

    @PostMapping("/user/getAllUserInfo")
    @ApiOperation(httpMethod = "POST", value = "获取所有用户信息")
    public ApiResponse<UserInfoVO> updateUserInfo(){
        List<UserInfoVO> userInfoVOS=new LinkedList<>();
        List<MatrixUser> list = userService.list(new LambdaQueryWrapper<MatrixUser>().orderByDesc(MatrixUser::getGmtCreate));
        list.stream().forEach(t->{
            UserInfoVO userInfoVO = BeanUtil.beanA2beanB(t, UserInfoVO.class);
            userInfoVO.setUserId(t.getId());
            userInfoVOS.add(userInfoVO);
        });
        return new ApiResponse(userInfoVOS);
    }


    @PostMapping("/user/addUserInfo")
    @ApiOperation(httpMethod = "POST", value = "添加用户信息")
    public ApiResponse<UserInfoVO> addUserInfo(@RequestBody UserAddPO po){
        MatrixUser byUsername = userService.getOne(new LambdaQueryWrapper<MatrixUser>().eq(MatrixUser::getUsername,po.getUsername()));
        if (byUsername==null){
            MatrixUser matrixUser = BeanUtil.beanA2beanB(po, MatrixUser.class);
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            matrixUser.setPassword(encoder.encode("123456"));
            userService.save(matrixUser);
        }else {
            throw new BusException("用户已存在");
        }
        return new ApiResponse("添加用户信息");
    }

    // 根据id进行更新
    @PostMapping("/user/getUserInfo")
    @ApiOperation(httpMethod = "POST", value = "获得用户信息")
    public ApiResponse<UserInfoVO> getUserInfo(@RequestBody UserInfoPO userInfoPO){
        System.out.println(userInfoPO.getUserId());
        MatrixUser byId = userService.getById(userInfoPO.getUserId());
        UserInfoVO userInfoVO = BeanUtil.beanA2beanB(byId, UserInfoVO.class);
        userInfoVO.setUserId(byId.getId());
        return new ApiResponse(userInfoVO);

    }

    @PostMapping("/user/deleteUserInfo")
    @ApiOperation(httpMethod = "POST", value = "删除用户信息")
    public ApiResponse<Void> deleteUserInfo(@RequestBody UserInfoPO userInfoPO){
        userService.removeById(userInfoPO.getUserId());
        return new ApiResponse().setReMsg("删除成功");
    }



}

